package com.sansi.pencilbook.service.impl;

import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Category;
import com.sansi.pencilbook.mapper.CategoryMapper;
import com.sansi.pencilbook.service.CategoryServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * 功能说明: 类别服务
 *
 * @author lsy13
 */
@Service
public class CategoryServerImpl implements CategoryServer {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Result getFirstCategory() {
        try {
            List<Category> list = categoryMapper.findByParent(0);
            return list.size()>0 ? Result.ofSuccess("查询类别成功", list.size(), list) :
                    Result.ofFailed("没有类别存在");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询类别失败");
        }
    }

    @Override
    public Result getAllCategories() {
        try {
            List<Category> list = categoryMapper.findAll();
            return list.size()>0 ? Result.ofSuccess("查询类别成功", list.size(), list) :
                    Result.ofFailed("没有类别存在");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询类别失败");
        }
    }

    @Override
    public Result getCategoriesByParent(int parentId) {
        try {
            List<Category> list = categoryMapper.findByParent(parentId);
            return list.size()>0 ? Result.ofSuccess("查询类别成功", list.size(), list) :
                    Result.ofFailed("该类别不存在子类");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询类别失败");
        }
    }

    @Override
    public Result getCategoryByName(String name) {
        try {
            Category category = categoryMapper.findByName(name);
            return category!=null ? Result.ofSuccess("查询成功", 1, category) :
                    Result.ofFailed("该类别不存在");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询类别失败");
        }
    }

    @Override
    public Result getCategoryById(int id) {
        try {
            Category category = categoryMapper.findById(id);
            return category!=null ? Result.ofSuccess("查询成功", 1, category) :
                Result.ofFailed("该类别不存在");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询类别失败");
        }
    }

    @Override
    public Result addCategory(Category category) {
        try {
            return Result.ofSuccess("增加类别成功", categoryMapper.saveCategory(category));
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("增加类别失败");
        }
    }

    @Override
    public Result modifyCategory(Category category) {
        try {
            return Result.ofSuccess("修改类别成功", categoryMapper.updateCategory(category));
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("修改类别失败");
        }
    }

    @Override
    public Result deleteCategory(Category category) {
        try {
            return Result.ofSuccess("删除类别成功", categoryMapper.deleteCategory(category));
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("删除类别失败");
        }
    }

    @Override
    public Result deleteCategories(List<Category> categories) {
        try {
            return Result.ofSuccess("删除类别成功", categoryMapper.deleteCategories(categories));
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("删除类别失败");
        }
    }
}
